<html>
<head><meta charset="utf-8"><title>How to create fake trait · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html">How to create fake trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216623068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216623068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216623068">(Nov 13 2020 at 15:02)</a>:</h4>
<p>Hello I'm trying to solve this issue <a href="https://github.com/rust-analyzer/rust-analyzer/issues/6492">https://github.com/rust-analyzer/rust-analyzer/issues/6492</a> so I have the Ty and now want to use this method <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir/src/code_model.rs#L1406:L1406">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir/src/code_model.rs#L1406:L1406</a> but I don't really know how I can create the <code>Trait</code> struct to correspond to my <code>Default</code> trait. Any idea to help me ? Thanks a lot</p>



<a name="216623387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216623387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216623387">(Nov 13 2020 at 15:04)</a>:</h4>
<p>You need to use <code>FamousDefs</code> to find <code>Default</code> <code>Trait</code></p>



<a name="216623673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216623673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216623673">(Nov 13 2020 at 15:06)</a>:</h4>
<p>Yes I already try that, but when using find_trait from FamousDefs, it returns None, Maybe I'm missing something ? Should I include something in my tests code or not ?</p>



<a name="216624046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624046">(Nov 13 2020 at 15:10)</a>:</h4>
<p>The <code>Default</code> trait itself should be present in the test code, yeah</p>



<a name="216624067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624067">(Nov 13 2020 at 15:10)</a>:</h4>
<p>Not sure if FamousDEfs::Fixture already has it</p>



<a name="216624120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624120">(Nov 13 2020 at 15:11)</a>:</h4>
<p>No not yet, But I implemented it. Will try to include fake trait in my test code :)</p>



<a name="216624170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624170">(Nov 13 2020 at 15:11)</a>:</h4>
<p>derive trait doesn't work in test code ?</p>



<a name="216624524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624524">(Nov 13 2020 at 15:14)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">default</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="nb">Default</span>: <span class="nb">Sized</span> <span class="p">{</span><span class="w"></span>
<span class="w">       </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">S</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foo</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="n">bar</span>: <span class="kt">usize</span> <span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">default</span>::<span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">foo</span>: <span class="mi">0</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">bar</span>: <span class="mi">0</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">process</span><span class="p">(</span><span class="n">f</span>: <span class="nc">S</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">other</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">foo</span>: <span class="mi">5</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="o">&lt;|&gt;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="216624549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624549">(Nov 13 2020 at 15:14)</a>:</h4>
<p>here is my test code</p>



<a name="216624647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216624647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216624647">(Nov 13 2020 at 15:15)</a>:</h4>
<p>and I added this in FamousDefs</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">default</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="nb">Default</span>: <span class="nb">Sized</span> <span class="p">{</span><span class="w"></span>
<span class="w">       </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>with the right export in prelude<br>
It's weird</p>



<a name="216632785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/How%20to%20create%20fake%20trait/near/216632785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Coenen Benjamin <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/How.20to.20create.20fake.20trait.html#216632785">(Nov 13 2020 at 16:09)</a>:</h4>
<p>Ok got it, in fact I have to include <code>FamousDefs::FIXTURES</code> inside my code test fixture, my bad</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>